app.controller("noteListCtrl",function($scope,$state,$ionicLoading,$http,noteListService,$timeout){
    var user_info = JSON.parse(localStorage.getItem("UserInfo"));
    var page = 0;
    var timer = null;
    // var getPath = JSON.parse(localStorage.getItem("getPath"));
    angular.element(document).ready(function () {
        $scope.loadMore();
    })

    var param = {
        userId:user_info.userId,
        pageSize:5,
        currentPage:page
    }
    $scope.noteList = [];
    $scope.loadMore = function () {

        if($scope.hasmore == true){
            return ;
        }
        page += 1;
        param.currentPage = page;
        noteListService.getNoteList(param)
            .then(function (data) {
                if(data.data.resultCode==200){
                    var res = data.data.resultBean.resultList;
                    if(res.length == 0){
                        $scope.hasmore = true;
                    }
                    Array.prototype.push.apply($scope.noteList, res);
                    timer = $timeout(function () {
                        $scope.$broadcast('scroll.infiniteScrollComplete');
                    }, 1500);
                }else{
                    $scope.hasmore = true;
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                }
            }).finally(function () {
                $timeout.cancel(timer);
                $scope.$broadcast('scroll.infiniteScrollComplete');
            });
    };
    $scope.doRefresh = function (getNoteName) {
        $scope.hasmore = false;
        page = 1;
        param.currentPage = page;
        if(getNoteName){
            param.bookName = getNoteName;
        }
        $scope.noteList = [];
        noteListService.getNoteList(param)
            .then(function (data) {
                if(data.data.resultCode == 300){
                    $ionicLoading.show({
                        template: "无笔记数据",
                        duration:3000
                    });
                    $scope.hasmore = true;
                }else{
                    if(data.data.resultCode==200){
                        var res = data.data.resultBean.resultList;
                        if(res.length == 0){
                            $scope.hasmore = true;
                        }
                        Array.prototype.push.apply($scope.noteList, res);
                        timer = $timeout(function () {
                            $scope.$broadcast('scroll.infiniteScrollComplete');
                        }, 1500);
                    }else{
                        $scope.hasmore = true;
                        $scope.$broadcast('scroll.infiniteScrollComplete');
                    }
                }
            }).finally(function () {
                $scope.$broadcast('scroll.infiniteScrollComplete');
        });
    };
    //获取笔记列表
    $scope.getNoteList = function (searchInput) {
        $ionicLoading.show({
            template: "加载数据中...",
        });
        param.bookName = searchInput;
        var url = getPath.url+getPath.name+"/app/getBookNoteByPage";
        $http({
            url: url,
            method: "POST",
            data:param,
            headers:{'Content-Type':'application/x-www-form-urlencoded'}
        }).success(function(data){
                if(data.resultCode==200){
                    return data.resultBean.resultList;
                }else{
                    $ionicLoading.show({
                        template: "没有更多笔记了",
                        duration:3000
                    });
                }
            })
            .error(function(data){
                $ionicLoading.show({
                    template: "获取笔记列表失败",
                    duration:3000
                });
            });
    }
    $scope.$on("$destroy", function () {
        $timeout.cancel(timer);
        //清除配置,不然scroll会重复请求
    });
    //笔记详情
    $scope.openNoteContent = function(noteNum){
        $state.go("home.noteContent",{
            noteNum:noteNum
        });
    }
});